using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[RequireComponent(typeof(CanvasGroup))]
// [RequireComponent(typeof(AlphaButtonClickMask))]
public class Draggable : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
    private RectTransform rectTransform;
    private CanvasGroup canvasGroup;

    private Color defc;

    private void Awake()
    {
        rectTransform = GetComponent<RectTransform>();
        canvasGroup = GetComponent<CanvasGroup>();
        defc = GetComponent<Graphic>().color;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        // 当开始拖拽时执行的操作
        canvasGroup.alpha = 0.6f; // 设置透明度或其他效果
        canvasGroup.blocksRaycasts = false; // 禁用射线检测，以便在拖拽时不会阻挡其他UI元素的交互
        transform.SetAsLastSibling();
        GetComponent<Graphic>().color = Color.yellow;
    }

    public void OnDrag(PointerEventData eventData)
    {
        // 当拖拽过程中执行的操作
        rectTransform.anchoredPosition += eventData.delta/transform.parent.localScale.x; // 更新UI元素的位置，使其跟随鼠标或触摸输入
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        // 当结束拖拽时执行的操作
        canvasGroup.alpha = 1f; // 恢复透明度或其他效果
        canvasGroup.blocksRaycasts = true; // 启用射线检测，以便在拖拽结束后可以与其他UI元素进行交互
        GetComponent<Graphic>().color = defc;
        SendMessage("DragEnds");
    }
}